package org.playground.booking.dao.impl;

import org.hibernate.query.Query;
import org.playground.booking.dao.IUserDao;
import org.playground.booking.entity.User;
import org.springframework.stereotype.Repository;

import java.util.Calendar;
import java.util.List;

/**
 * Created by root on 17-10-28.
 */
@Repository
public class UserDaoImpl extends AbstractDaoImpl<User,Long> implements IUserDao {

   public User readUserByOpenId(String openId){
       String hql = "from User " +
               "where openId =:openId";
       Query query = this.getCurrentSession().createQuery(hql);
       query.setParameter("openId",openId);

       List<User> result = query.list();
       return result.size()==0?null:result.get(0);
   }

   public void createUserByOpenId(String openId){
       User user = new User();
       user.setOpenId(openId);
       Long suffix = Calendar.getInstance().getTimeInMillis();
       user.setName("anonymous"+suffix);

       this.save(user);
   }

}
